package com.huobao.yunying.activitys.muyingquan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_SheQuTieZi;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.UserInfo;
import com.huobao.yunying.entity.User_buyer_stat;
import com.huobao.yunying.entity.Userugrade;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.UserInfoUtils;
import com.huobao.yunying.utils.UserUtils;
import com.huobao.yunying.views.RoundImageView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ExitUtil;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的社区
 * 
 * @author Administrator
 *
 */
public class Act_My_Shequ extends HuobaoBaseActivity implements OnClickListener,OnItemClickListener {
	// 发帖
	@ViewInject(R.id.tv_fatie)
	private TextView Tv_FaTie;
	@ViewInject(R.id.lin_fatie)
	private LinearLayout Linear_FaTie;
	// 回帖
	@ViewInject(R.id.tv_huitie)
	private TextView Tv_HuiTie;
	@ViewInject(R.id.lin_huitie)
	private LinearLayout Linear_HuiTie;
	// 消息
	@ViewInject(R.id.tv_xiaoxi)
	private TextView Tv_XiaoXi;
	@ViewInject(R.id.lin_xiaoxi)
	private LinearLayout Linear_XiaoXi;
	// 收藏
	@ViewInject(R.id.tv_shoucang)
	private TextView Tv_ShouCang;
	@ViewInject(R.id.lin_shoucang)
	private LinearLayout Linear_ShouCang;
	// 显示最近回帖的列表
	@ViewInject(R.id.shequlistview)
	private ListView mListView;
	// 当最近发帖的内容为空的时候
	@ViewInject(R.id.shequempty)
	private LinearLayout mLinear_empty;
	// 当最近发帖有内容的时候
	@ViewInject(R.id.shequdata)
	private LinearLayout mLinear_data;
	// 个人头像
	@ViewInject(R.id.img_userphoto)
	private RoundImageView Img_UserPhoto;
	// 个人昵称
	@ViewInject(R.id.shequ_name)
	private TextView Tv_Name;
	// 圈龄
	@ViewInject(R.id.shequ_quanling)
	private TextView Tv_QuanLing;
	private Adapter_SheQuTieZi adapter;
	private List<SheQuTieZi> datas = new ArrayList<SheQuTieZi>();

	@ViewInject(R.id.llyt_30)
	private View view_my;

	@Override
	protected int getContentView() {
		return R.layout.layout_myshequ;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	@Override
	protected void init() {
		EventBus.getDefault().register(this);
		mAbTitleBar.setVisibility(View.GONE);
		//设置头像 昵称 圈龄
		Img_UserPhoto.setRectAdius(90);
		UserInfo info = UserInfoUtils.getUserInfo(context);
		if(info != null){
			Tv_Name.setText(TextUtils.isEmpty(info.nikename) ? info.user_name : info.nikename);
			ImageUtils.loadimg(Img_UserPhoto, info.portrait);
		}else{
			getUserMsg();
		}
		
		// 获取回帖发帖收藏的数目
		GetSheQuNum();
		adapter = new Adapter_SheQuTieZi(context, R.layout.item_shequ_tiezi);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		GetZuiJinFaTie();
		Linear_FaTie.setOnClickListener(this);
		Linear_HuiTie.setOnClickListener(this);
		Linear_XiaoXi.setOnClickListener(this);
		Linear_ShouCang.setOnClickListener(this);
		view_my.setOnClickListener(this);
	}
	
	private void getUserMsg() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "member");
		Http.getInstant().sendRequest(this, false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				UserInfo user = JSON.parseObject(jsonObject.getString("user_info"), UserInfo.class);
				UserInfoUtils.saveUserInfo(context, user);
				Tv_Name.setText(TextUtils.isEmpty(user.nikename) ? user.user_name : user.nikename);
				ImageUtils.loadimg(Img_UserPhoto, user.portrait);
			}

			@Override
			public void onHttpError(String error) {
				EventBus.getDefault().post("", "GetUserMessageFail");
			}
		});
	}

	/**
	 * 获取我的社区页面的 回帖数 收藏数 发帖数
	 */
	private void GetSheQuNum() {
		Map<String, String> map = HttpUtil.getParams("app", "mypost", "act", "count");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				SheQuNum num = JSON.parseObject(jsonObject.getString("retval"), SheQuNum.class);
				Tv_QuanLing.setText("圈龄："+num.getRing_age());
				Tv_FaTie.setText(num.getPost_count());
				Tv_HuiTie.setText(num.getReplies_count());
				Tv_ShouCang.setText(num.getCollect_count());
				Tv_XiaoXi.setText(num.getMessage_count());
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	/**
	 * 获取我的社区页面的最近发帖的帖子
	 */
	private void GetZuiJinFaTie() {
		Map<String, String> map = HttpUtil.getParams("app", "mypost", "act", "week_post");
		Http.getInstant().sendRequest(context, true, map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				datas = jsonObject.parseArray(jsonObject.getString("retval"), SheQuTieZi.class);
				if (datas == null || datas.size() == 0) {
					mLinear_empty.setVisibility(View.VISIBLE);
					mLinear_data.setVisibility(View.GONE);
				} else {
					mLinear_empty.setVisibility(View.GONE);
					mLinear_data.setVisibility(View.VISIBLE);
					adapter.clear();
					adapter.addAll(datas);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	@Subscriber(tag = "refresh")
	public void refresh(String str){
		GetSheQuNum();
		GetZuiJinFaTie();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lin_fatie:// 发帖
			startActivity(new Intent(Act_My_Shequ.this, Act_SheQu_FaTie.class));
			break;
		case R.id.lin_huitie:// 回帖
			startActivity(new Intent(Act_My_Shequ.this, Act_SheQu_HuiTie.class));
			break;
		case R.id.lin_xiaoxi:// 消息
			startActivity(new Intent(Act_My_Shequ.this, Act_SheQu_XiaoXi.class));
			break;
		case R.id.lin_shoucang:// 收藏
			startActivity(new Intent(Act_My_Shequ.this, Act_SheQu_ShouCang.class));
			break;
		case R.id.llyt_30:
			EventBus.getDefault().post("", "skipToMine");
			break;
		}
	}
	private long tempTime = 0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(tempTime == 0){
				tempTime = System.currentTimeMillis();
				Toast.makeText(context, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
				return false;
			}
			if((System.currentTimeMillis() - tempTime) <= 2 * 1000){
				//退出程序-
				ExitUtil.getInstant().exitApp(context);
				finish();
				return false;
			}
			Toast.makeText(context, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
			tempTime = System.currentTimeMillis();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, Act_Tiezi_Detail.class);
		intent.putExtra("id", adapter.getItem(position).getPid());
		intent.putExtra("quanziid", adapter.getItem(position).getGid());
		startActivity(intent);
		
	}
}
